package com.wmh.baseservice.merchant.controller;

import com.wmh.baseservice.common.utils.CommUtil;
import com.wmh.baseservice.common.utils.page.pojo.PageInfo;
import com.wmh.baseservice.common.utils.page.pojo.PageResult;
import com.wmh.baseservice.common.utils.page.pojo.SortWay;
import com.wmh.baseservice.common.utils.page.util.PageUtil;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.merchant.pojo.MerchantPlotRent;
import com.wmh.baseservice.user.pojo.UsInfo;
import com.wmh.baseservice.user.service.UsInfoService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import com.wmh.baseservice.merchant.service.MerchantPlotRentService;

/**
 * @author wmh generator
 * @since 2022-01-17
 */
@Api(tags = "商家端-租赁相关接口")
@RestController
@RequestMapping(value = "merchant/merchantPlotRent", produces = "text/plain;charset=utf-8")
@RequiresPermissions(logical = Logical.OR,value = {"租赁套餐管理操作","所有权限"})
public class MerchantPlotRentController {

    @Resource
    private MerchantPlotRentService merchantPlotRentService;
    @Resource
    private UsInfoService usInfoService;

    @ApiOperation("已租出的区域")
    @PostMapping("leaseList")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "id", value = "id", required = false),
    })
    public String leaseList(PageInfo pageInfo) {
        pageInfo.setTimeScreen("create_time");
        pageInfo.setDefaultSort("create_time", SortWay.DESC);
        //执行分页辅助工具
        PageResult<MerchantPlotRent> pageResult = new PageUtil<MerchantPlotRent>(pageInfo).startPage((page, queryWrapper) -> {
            merchantPlotRentService.page(page, queryWrapper);
        });
        pageResult.getList().forEach(item -> {
            UsInfo usInfo = usInfoService.getByIdForUpdate(item.getRentUsId());
            if (CommUtil.checkNull(usInfo)) {
                item.setNick(usInfo.getNick());
                item.setAvatar(usInfo.getAvatar());
            }
        });
        return ReturnBody.success(pageResult);
    }
}
